/*
 * @Description: 订单模块控制器
 */
const orderCtrl = require("../model/controller/orderCtrl");
const shoppingCartCtrl = require("../model/controller/shoppingCartCtrl");
const productCtrl = require("../model/controller/productCtrl");
const checkUser = require("./utils/checkUser");

module.exports = {
  /**
   * 获取用户的所有订单信息
   */
  async GetOrder(req, res) {
    let { user_id } = req.body;
    let result;
    // 校验用户是否登录
    // if (!checkUser(req, res)) {
    //   return;
    // }
    // 获取所有的订单id,[order_id1,order_id2]
    const ordersGroup = await orderCtrl.GetOrderGroup(user_id);

    // 该用户没有订单,直接返回信息
    if (ordersGroup.length == 0) {
      result = {
        code: "002",
        msg: "该用户没有订单信息",
      };
      res.send(result);
      return;
    }

    // 获取所有的订单详细信息
    const orders = await orderCtrl.GetOrder(user_id);

    let ordersList = [];
    // 生成每个订单的详细信息列表
    for (let i = 0; i < ordersGroup.length; i++) {
      const orderID = ordersGroup[i];
      let tempOrder = [];

      for (let j = 0; j < orders.length; j++) {
        const order = orders[j];

        if (orderID.order_id == order.order_id) {
          // 获取每个商品详细信息
          const product = await productCtrl.GetProductById(order.product_id);
          order.product_name = product.product_name;
          order.product_picture = product.product_picture;

          tempOrder.push(order);
        }
      }
      ordersList.push(tempOrder);
    }

    result = {
      code: "001",
      orders: ordersList,
    };
    res.send(result);
  },
  /**
   * 添加用户订单信息
   */
  async AddOrder(req, res) {
    let { user_id, products } = req.body;

    // 校验用户是否登录
    // if (!checkUser(req, res)) {
    //   return;
    // }

    // 获取当前时间戳
    const timeTemp = new Date().getTime();
    // 生成订单id：用户id+时间戳(string)
    const orderID = +("" + user_id + timeTemp);
    let data = [];
    // 根据数据库表结构生成字段信息
    for (let i = 0; i < products.length; i++) {
      const temp = products[i];
      let product = {
        order_id:orderID,
        product_id:temp.productID,
        user_id:user_id,
        product_num:temp.num,
        product_price:temp.price,
        order_time:timeTemp
      }
      data.push(product);
    }

    // 把订单信息插入数据库
    const insertNum = await orderCtrl.AddOrder(data);

    let result;
    // 插入成功
    if (insertNum == products.length) {
      //删除购物车
      let rows = 0;
      for (let i = 0; i < products.length; i++) {
        const temp = products[i];
        const res = await shoppingCartCtrl.DeleteShoppingCart(
          user_id,
          temp.productID
        );
        rows += res;
      }
      //判断删除购物车是否成功
      if (rows != products.length) {
        result = {
          code: "002",
          msg: "购买成功,但购物车没有更新成功",
        };
        res.send(result);
        return;
      }

      result = {
        code: "001",
        msg: "购买成功",
      };
    } else {
      result = {
        code: "004",
        msg: "购买失败,未知原因",
      };
    }
    res.send(result);
    return;
  },
};
